﻿var dialog_opendiv;
function searchtype(type, inputId) {
    var hideInputId = '';
    var typearr = [lang.Object_list, lang.Business_list];
    if (type == 0) hideInputId = "targetValue";
    else
        hideInputId = "bizValue";
    $('#' + hideInputId).val('');

    var _html = [];
    if (type == 0) {

        _html.push('<table id="tt"  class="easyui-treegrid" style="height:390px; overflow:auto " rownumbers="true" idField="id" treeField="name">');
        _html.push('  <thead>');
        _html.push('   <tr>');
        _html.push('  <th field="name" width="650"></th>');
        _html.push('  </tr>');
        _html.push('</thead>');
        _html.push('</table>');

        dialog_opendiv = popwindows({ html: _html.join(''), title: typearr[type], width: 500, height: 420 });

        $('#tt').treegrid({
            url: '../Handlers/getTargetNodes.aspx',

            onDblClickCell: function (field, row) { if (!isexist(inputId, hideInputId, row.id)) { $('#' + inputId).val($('#' + inputId).val() + " " + row.name); } }

        }

     );

    }
    else {
        _html.push('<div>')
        _html.push('<div style="overflow:auto;height:390px;">')
        _html.push('<div id="tree">');
        _html.push('</div>');
        _html.push('</div>');
        //_html.push('<div style="border-top:1px solid #ccc;margin:0 atuo;padding:15px 0 0 0;text-align:center;"><span style="padding:5px;border:1px solid #eee;background:#f8f8f8;margin:0 30px;cursor:pointer;" onclick="clearselect(' + type + ')">清空选择</span><span  style="padding:5px;border:1px solid #eee;background:#f8f8f8;cursor:pointer;"  onclick="selectnode(' + type + ')">确定选择</span></div>')
        _html.push('</div>');

        dialog_opendiv = popwindows({ html: _html.join(''), title: typearr[type], width: 500, height: 420 });

        $('#tree').tree({ 'checkbox': false, onDblClick: function (node) { if (!isexist(inputId, hideInputId, node.id)) { $('#' + inputId).val($('#' + inputId).val() + " " + node.text); } } });
        gettreelist(type);
    }
}

//#region 加载菜单数据
function gettreelist(typenum) {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryAllTree",
        model: function () { return typenum; },
        success: function (data) {
            if (data && data.length > 0) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PObjectID == '0') {
                        var obj = {};
                        obj.id = data[i].ObjectID;
                        obj.text = data[i].ObjectName;
                        getsubtree(data, data[i].ObjectID, obj);
                        _data.push(obj);
                    }
                }
                $('#tree').tree('loadData', _data);
                //绑定选中
                if (typenum == 0)
                    idstr = "targetValue";
                else
                    idstr = "bizValue";
            }

        }
    });
    //6b5.testdsssfes15s541a12s
    function getsubtree(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PObjectID == _pid) {
                    var obj = {};
                    obj.id = _d[i].ObjectID;
                    obj.text = _d[i].ObjectName;
                    _sdata.push(obj);
                }
            }

            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubtree(_d, _sdata[j].id, _sdata[j]);
                }
            }
        }
    }
}

//清空节点
function clearselect(type) {
    if (type == 0)
        idstr = "targetValue";
    else
        idstr = "bizValue";
    $("#" + idstr).val('');
    dialog_opendiv.close();
}

//确定选择节点
function selectnode(type) {
    var nodes = $('#tree').tree('getChecked')
    var _selectnode = [];
    for (var i = 0; i < nodes.length; i++) {
        _selectnode.push(nodes[i].id);
    }
    var idstr = '';
    if (type == 0)
        idstr = "targetValue";
    else
        idstr = "bizValue";
    $("#" + idstr).val(_selectnode.join());
    dialog_opendiv.close();
}

function isexist(inputId, hideid, val) {
    var _vals = $('#' + hideid).val();
    if (_vals == '') {
        $('#' + hideid).val($('#' + hideid).val() + ',' + val);
        return false;
    }
    else {
        var _valarr = _vals.split(',');
        if ($.inArray('' + val, _valarr) > -1) {
            return true;
        }
        else {
            $('#' + hideid).val($('#' + hideid).val() + ',' + val);
            return false;
        }
    }
}
